﻿Imports System.Xml.Serialization
Imports System.IO

Public Class Game
	Public Maps As New Hashtable()
	Public CurrentMap As Map

	Public Player As New Player()

	Public MapTiles As New Hashtable()


	Public Sub New()

	End Sub

	'Add all the tiles here, {filename, an unique ID, T/F on whether its solid}
	Public Sub AddTiles()
		'Ground Tiles
		AddTile("BMP\Tiles\Outside\Grass.bmp", "Grass", False)

		'Roads Tiles
		AddTile("BMP\Tiles\Outside\Road1.bmp", "Road1", False)
		AddTile("BMP\Tiles\Outside\Road2.bmp", "Road2", False)
		AddTile("BMP\Tiles\Outside\Road3.bmp", "Road3", False)
		AddTile("BMP\Tiles\Outside\RoadCorner1.bmp", "RoadCorner1", False)
		AddTile("BMP\Tiles\Outside\RoadCorner2.bmp", "RoadCorner2", False)
		AddTile("BMP\Tiles\Outside\RoadCorner3.bmp", "RoadCorner3", False)
		AddTile("BMP\Tiles\Outside\RoadCorner4.bmp", "RoadCorner4", False)
		AddTile("BMP\Tiles\Outside\RoadT1.Bmp", "RoadT1", False)
		AddTile("BMP\Tiles\Outside\RoadT2.Bmp", "RoadT2", False)
		AddTile("BMP\Tiles\Outside\RoadT3.Bmp", "RoadT3", False)
		AddTile("BMP\Tiles\Outside\RoadT4.Bmp", "RoadT4", False)

		'Outside Object Tiles
		AddTile("BMP\Tiles\Outside\Tree.bmp", "Tree", True)
		AddTile("BMP\Tiles\Outside\Rock.bmp", "Rock", True)


	End Sub

	Public Sub AddTile(FileName As String, tileid As String, solid As Boolean)
		Try
			Dim stream As New IO.FileStream(FileName, IO.FileMode.Open)
			Dim maptile As New MapTile(New Bitmap(stream), tileid, solid)
			MapTiles(tileid) = maptile
			stream.Close()
		Catch ex As Exception
			Stop
		End Try
	End Sub

	Public Sub LoadMap(filename As String)
		Dim serializer As New XmlSerializer(GetType(Map))
		Dim fs As New FileStream(filename, FileMode.Open)
		Dim Map As Map = CType(serializer.Deserialize(fs), Map)
		fs.Close()
		Map.FinishLoad()
		For x As Integer = 0 To Map.Width - 1
			For y As Integer = 0 To Map.Height - 1
				Map.MapTileMatrix(x)(y).CopyFrom(MapTiles(Map.MapTileMatrix(x)(y).TileID))
			Next
		Next

		Maps(Map.MapID) = Map
		If CurrentMap Is Nothing Then
			CurrentMap = Map
		End If

	End Sub

	Public Sub Update()
		CurrentMap.Update()
		CurrentMap.Graphics.DrawImage(Player.tile.BitMap, Player.LocX * MapTile.Width, Player.LocY * MapTile.Height, MapTile.Width, MapTile.Height)
	End Sub
	Public Sub LoadGame()
		AddTiles()
		Player.LoadPlayer()
		LoadMap("Map/Maps/Map002.map")
		CurrentMap.StartX = 8
		CurrentMap.StartY = 15
		Player.LocX = CurrentMap.StartX
		Player.LocY = CurrentMap.StartY

	End Sub
End Class
